from google.appengine.ext import webapp
from web.constants import get_admin_user, title, slogan, menu, ARTICLES_ON_PAGE, \
    cleaner
from appengine_utilities import sessions
from web.models import ArticleModel, CommentModel
from google.appengine.ext.webapp import template
import uuid
import os
from web.translate import translate
from google.appengine.api import mail
from recaptcha.client import captcha


def send_email_to_admin(text):     
    mail.send_mail(sender="ivan.ivanenko@gmail.com",
              to="ivan.ivanenko@gmail.com",
              subject="New comment",
              body=text)

class DeleteComment(webapp.RequestHandler):
    def get(self):
        cuuid = self.request.get('cuuid')
        u_id = self.request.get('uuid')
        lang = self.request.get('lang')
        if cuuid:
            cm = CommentModel().get(cuuid)
            cm.delete()
        self.redirect("/article/" + u_id + "/" + lang)
        

class AddComment(webapp.RequestHandler):
    def post(self):
        self.get()
    def get(self):
        #self.response.out.write("Comments templorary disabled!")
        #return "Comments"
        """"comments"""
        name = self.request.get('name')
        site = self.request.get('site')
        comment = self.request.get('comment')
        u_id = self.request.get('uuid')
        lang = self.request.get('lang')
        
        if not lang:
            lang = "rus"
        
      
        
        try:
            article = ArticleModel.get_by_id(int(u_id))
        except:
            self.redirect("/page_404")
            return None
        
        if not article:
            self.redirect("/page_404")
            return None
        
        """verify captcha"""
        remote_ip = self.request.remote_addr
        challenge = self.request.get('recaptcha_challenge_field')
        response = self.request.get('recaptcha_response_field')
        recaptcha_response = captcha.submit(challenge, response, "6Ld9tL0SAAAAAA8RPX--P6dgmyJ2HUeUdBUfLLAM", remote_ip)
        
        
        link = "/article/" + u_id + "/" + lang
        
        if not recaptcha_response.is_valid:
            #self.response.out.write("Not valid == "+str(recaptcha_response.error_code) + " == ip:" + remote_ip+" == chall: "+challenge+" == resp: "+response)
            self.redirect(link + "?name=%s&site=%s&comment=%s" % (name, site, comment))            
            return
        
        if name and comment:          
            session = sessions.Session()
            if not session.has_key('user_id'):
                session['user_id'] = uuid.uuid4().hex
                          
            cm = CommentModel()
            cm.article = article
            cm.name = cleaner.strip(name[:100])
            
            if not site:
                site = "http://foobnix.com"
            if site and not site.startswith("http://"):
                site = "http://" + site
            cm.site = cleaner.strip(site[:100])       
            comment = cleaner.strip(comment[:500])
            cm.comment = comment
            cm.comment_eng = translate(comment, src="ru", to="en")
            cm.user_id = session['user_id']
            cm.put()
            send_email_to_admin("@" + name + " " + comment + " http://www.foobnix.com" + link)
        
        self.redirect(link)
        
            
